//
//  CZQphotoShowTableViewCell.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/13.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQphotoShowTableViewCell.h"
@interface CZQphotoShowTableViewCell ()<SDPhotoBrowserDelegate>

@end
@implementation CZQphotoShowTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    if (self.section == 2) {
        
        
    
    }else
    {
        
    }
    
}

-(void)setArrM:(NSMutableArray *)arrM
{
    
    _arrM = arrM;
    
    _imageView = nil;
    
    
    if (arrM.count > 0) {
        
        self.addBtn .hidden = YES;
        
    }else
    {
        self.addBtn .hidden = NO;
        
    }
    
   
        
    //擅长
    if (self.section == 3) {
        
        for (UIView *view in self.scrollView.subviews) {
            
            if ([view isKindOfClass:[CZQLongBtnView class]] || [view isKindOfClass:[UIImageView class]]) {
                [view removeFromSuperview];
            }
            
        }
        
        //记录前一个View
        CZQLongBtnView *longView = nil;
        
        CGFloat width ;
        
        for (int i = 0 ; i < arrM.count; i ++ ) {
            
            CZQLongBtnView *view = [CZQLongBtnView new];
            
            view.tag = 200 + i;
            //删除
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeLong:)];
            
            [view addGestureRecognizer:tap];
            
            view.longLable.text = arrM[i];
            
            [self.scrollView addSubview:view];
            
            CGFloat w = [Helper widthOfString:arrM[i] font:[UIFont systemFontOfSize:15] height:30] + 50;
            
            width = width + w;
            
            if (i == 0) {
                
                [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.centerY.mas_equalTo(view.superview);
                    make.left.mas_equalTo(view.superview).offset(10);
                    make.height.mas_equalTo(50);
                    make.width.mas_equalTo(w);
                    
                    
                }];
                
            }else if (i == arrM.count - 1){
                
                [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.height.mas_equalTo(longView);
                    make.left.mas_equalTo(longView.mas_right).offset(10);
                    make.width.mas_equalTo(w);
                    
                }];
                
            }else
            {
                [view mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.height.mas_equalTo(longView);
                    make.left.mas_equalTo(longView.mas_right).offset(10);
                    make.width.mas_equalTo(w);
                    
                }];
                
            }
            
            longView = view;
            
        }
        
        
        
        self.scrollView.contentSize = CGSizeMake(width + arrM.count *11, 0);
        
    }else
    {
        
        
        for (UIView *view in self.scrollView.subviews) {
            
            if ([view isKindOfClass:[UIImageView class]] || [view isKindOfClass:[CZQLongBtnView class]]) {
                [view removeFromSuperview];
            }
            
        }
        
        //记录前一个view
        UIImageView *imgView = nil;
        
        for (int i = 0; i < arrM.count; i ++) {
            
            DHCustomImageView *img = [DHCustomImageView new];
            img.deleteBtn.tag = 500 + i;
            [img.deleteBtn addTarget:self action:@selector(deleteImg:)];
//            img.image = arrM[i];
            [img sd_setImageWithURL:[Helper imageUrlStringToChinese:[NSURL URLWithString:arrM[i]] imageType:nil] placeholderImage:PlaceHold];
            img.userInteractionEnabled = YES;
            [self.scrollView addSubview:img];
            img.tag = 300 + i;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showPhoto:)];
            [img addGestureRecognizer:tap];
            
            
            if (i == 0) {
                
                [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.left.mas_equalTo(img.superview).offset(10);
                    make.bottom.mas_equalTo(img.superview).offset(-10);
                    make.width.mas_equalTo(img.mas_height);
                    
                    make.height.mas_equalTo(self.width/4);
                    
                }];
                
            }else if (i == arrM.count - 1){
                
                [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.bottom.mas_equalTo(imgView);
                    make.left.mas_equalTo(imgView.mas_right).offset(20);
                    
                    make.width.height.mas_equalTo(imgView);
                    
                }];
                
            }else
            {
                [img mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.bottom.mas_equalTo(imgView);
                    make.left.mas_equalTo(imgView.mas_right).offset(20);
                    
                    make.width.height.mas_equalTo(imgView);
                    
                }];
                
            }
            imgView = img;
            
            
        }
         self.scrollView.contentSize =CGSizeMake((imgView.height + 20) *arrM.count + 20, 0);
    }
    
    [self setNeedsLayout];
    [self setNeedsDisplay];
    
//    self.scrollView.contentSize =CGSizeMake(50 *arrM.count + 20, 0);
    
    
}

-(void)setModel:(CZQResumeModel *)model
{
    
    _model = model;
    
}


#pragma mark -删除擅长
-(void)removeLong:(UITapGestureRecognizer *)tap
{
    CZQLongBtnView *view = tap.view;
    
    [self.arrM removeObjectAtIndex:view.tag -200];
    
    [self setArrM:self.arrM];
    
    if (self.section == 3) {
        
        if (self.arrM.count == 0) {
            self.model.FavTags = @"";
        }else
        {
            self.model.FavTags = [self.arrM componentsJoinedByString:@","];
        }
        
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(CZQphotoShowTableViewCellRefreashData)]) {
        [_delegate CZQphotoShowTableViewCellRefreashData];
    }
    
}

- (void)showPhoto:(UITapGestureRecognizer *)tap
{
    UIView *imageView = tap.view;
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.currentImageIndex = imageView.tag - 300;
    browser.sourceImagesContainerView = imageView;
    
    browser.imageCount = self.arrM.count;
    browser.delegate = self;
    [browser show];
    
    browser.removeBtn.hidden = NO;
}

#pragma mark - 删除图片
-(void)deleteImg:(UIButton *)sender
{
    [self.arrM removeObjectAtIndex:sender.tag - 500];
    
    [self setArrM:self.arrM];
    
    if (self.arrM.count == 0) {
        self.model.Pics = @"";
    }else
    {
        self.model.Pics = [self.arrM componentsJoinedByString:@","];
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(CZQphotoShowTableViewCellRefreashData)]) {
        [_delegate CZQphotoShowTableViewCellRefreashData];
    }
}
#pragma mark - SDPhotoBrowserDelegate

-(void)photoBrowser:(SDPhotoBrowser *)browser removePhoto:(NSInteger)index
{
    [self.arrM removeObjectAtIndex:index];
    
    [self setArrM:self.arrM];
    
    if (self.arrM.count == 0) {
        self.model.Pics = @"";
    }else
    {
        self.model.Pics = [self.arrM componentsJoinedByString:@","];
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(CZQphotoShowTableViewCellRefreashData)]) {
        [_delegate CZQphotoShowTableViewCellRefreashData];
    }
    
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    //    _index = index;
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",self.arrM[index] ,mid_image];
    urlStr=[urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:urlStr];
    return url;
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    //    UIImageView *imageView ;
    
    return PlaceHold;
}


-(void)layoutSubviews{
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
